package com.zhuozhou.jdyc.main.meinfo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivityOrderBinding;
import com.zhuozhou.jdyc.main.main.adapter.IndexTabMenuAdapter;
import com.zhuozhou.jdyc.main.main.bean.TabItemBean;
import com.zhuozhou.jdyc.main.meinfo.adapter.OrderAdapter;
import com.zhuozhou.jdyc.main.meinfo.bean.BasicsMyPayBean;
import com.zhuozhou.jdyc.main.meinfo.viewmodel.MeInfoViewModel;
import com.zhuozhou.jdyc.main.settlement.activity.VerificationActivity;
import com.zhuozhou.jdyc.util.log.ToastUtils;
import com.zhuozhou.jdyc.wight.dialog.ReturnGoodsDialog;
import com.zhuozhou.jdyc.wight.recyclerview.FullyGridLayoutManager;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Create by Once_ChuXy on 2021/9/18
 * Describe:我的订单
 */
public class OrderActivity extends BaseActivity<MeInfoViewModel, ActivityOrderBinding> implements OrderAdapter.OnItemButtonClickListener {

    private IndexTabMenuAdapter indexTabMenuAdapter;
    private OrderAdapter orderAdapter;
    private List<TabItemBean> tabMenuList = new ArrayList<>();
    private List<BasicsMyPayBean.ListBeanX> qbList = new ArrayList<>();
    private List<BasicsMyPayBean.ListBeanX> fkList = new ArrayList<>();
    private List<BasicsMyPayBean.ListBeanX> fhList = new ArrayList<>();
    private List<BasicsMyPayBean.ListBeanX> shList = new ArrayList<>();
    private List<BasicsMyPayBean.ListBeanX> pjList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order;
    }

    @Override
    public String setTitleBar() {
        return "我的订单";
    }

    @Override
    public void initView() {
        tabMenuList = new ArrayList<>();
        tabMenuList.add(new TabItemBean(0, "全部", baseBundle.getInt("type") == 0));
        tabMenuList.add(new TabItemBean(1, "待付款", baseBundle.getInt("type") == 1));
        tabMenuList.add(new TabItemBean(2, "待发货", baseBundle.getInt("type") == 2));
        tabMenuList.add(new TabItemBean(3, "待收货", baseBundle.getInt("type") == 3));
        tabMenuList.add(new TabItemBean(4, "待评价", baseBundle.getInt("type") == 4));

        mViewModel.BasicsMyPayMutableLiveData.observe(this, v -> {
            indexTabMenuAdapter = new IndexTabMenuAdapter(this);
            indexTabMenuAdapter.setList(tabMenuList);
            indexTabMenuAdapter.setTextColor(R.color.grey_700);
            indexTabMenuAdapter.setViewWidth(binding.mRecyclerViewTopMenu.getWidth() / 5);
            indexTabMenuAdapter.setOnTabMenuItemClickListener((view, position) -> {
                switch (position) {
                    case 0:
                        setOrderAdapter(qbList, 0);
                        break;
                    case 1:
                        setOrderAdapter(fkList, 1);
                        break;
                    case 2:
                        setOrderAdapter(fhList, 2);
                        break;
                    case 3:
                        setOrderAdapter(shList, 3);
                        break;
                    case 4:
                        setOrderAdapter(pjList, 4);
                        break;
                }
            });
            binding.mRecyclerViewTopMenu.setAdapter(indexTabMenuAdapter);
            binding.mRecyclerViewTopMenu.setLayoutManager(new FullyGridLayoutManager(this, 5) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            for (BasicsMyPayBean.ListBeanX item : v.getList()) {
                if (Integer.parseInt(item.getHexiao_status()) == 0) {
                    qbList.add(item);
                }
                if (Integer.parseInt(item.getStatus()) == 0 && Integer.parseInt(item.getShoppingtype()) == 0 && Integer.parseInt(item.getUsertype()) == 0 && Integer.parseInt(item.getDistribution()) != 2) {
                    fkList.add(item);
                }
                if (Integer.parseInt(item.getStatus()) == 1 && Integer.parseInt(item.getShoppingtype()) == 0 && Integer.parseInt(item.getUsertype()) == 0) {
                    fhList.add(item);
                }
                if (Integer.parseInt(item.getStatus()) == 1 && Integer.parseInt(item.getShoppingtype()) == 1 && Integer.parseInt(item.getUsertype()) == 0) {
                    shList.add(item);
                }
                if ((Integer.parseInt(item.getStatus()) == 1 && Integer.parseInt(item.getShoppingtype()) == 1 && Integer.parseInt(item.getUsertype()) == 1)
                        || (Integer.parseInt(item.getDistribution()) == 2 && Integer.parseInt(item.getHexiao_status()) == 1)) {
                    pjList.add(item);
                }
            }
            Collections.sort(qbList);
            Collections.sort(fkList);
            Collections.sort(fhList);
            Collections.sort(shList);
            Collections.sort(pjList);
            switch (baseBundle.getInt("type")) {
                case 0:
                    setOrderAdapter(qbList, 0);
                    break;
                case 1:
                    setOrderAdapter(fkList, 1);
                    break;
                case 2:
                    setOrderAdapter(fhList, 2);
                    break;
                case 3:
                    setOrderAdapter(shList, 3);
                    break;
                case 4:
                    setOrderAdapter(pjList, 4);
                    break;
            }
        });
        mViewModel.BasicsConsigneeMutableLiveData.observe(this, v -> {
            ToastUtils.showShort(v.getMsg());
            mViewModel.getBasicsMyPay(UserAccountHelper.getUserID() + "");
        });
        mViewModel.BasicsPayRefundMutableLiveData.observe(this, v -> {
            ToastUtils.showShort(v.getMsg());
            mViewModel.getBasicsMyPay(UserAccountHelper.getUserID() + "");
        });
        mViewModel.BasicsDelPayMutableLiveData.observe(this, v -> {
            ToastUtils.showShort(v.getMsg());
            mViewModel.getBasicsMyPay(UserAccountHelper.getUserID() + "");
        });
    }

    public void setOrderAdapter(List<BasicsMyPayBean.ListBeanX> list, int type) {

        if (list == null || list.size() == 0) {
            binding.tvNoOrder.setVisibility(View.VISIBLE);
            binding.mRecyclerViewOrder.setVisibility(View.GONE);
        } else {
            binding.tvNoOrder.setVisibility(View.GONE);
            binding.mRecyclerViewOrder.setVisibility(View.VISIBLE);
            orderAdapter = new OrderAdapter(this);
            orderAdapter.setType(type);
            orderAdapter.setList(list);
            orderAdapter.setOnItemButtonClickListener(this);
            binding.mRecyclerViewOrder.setAdapter(orderAdapter);
            binding.mRecyclerViewOrder.setLayoutManager(new MyLayoutManager(this));
        }
    }

    @Override
    public void initData(Bundle bundle) {
        mViewModel.getBasicsMyPay(UserAccountHelper.getUserID() + "");
    }


    @Override
    public void OnItemButtonClick(Integer itemPos, int pos, int type) {
        switch (type) {
            case 0: //晒单评价
                ToastUtils.showShort("暂不支持");
                break;
            case 1: //再次购买
                ToastUtils.showShort("暂不支持");
                break;
            case 2://取消订单
                mViewModel.postBasicsDelPay(orderAdapter.getList().get(pos).getPayid(), UserAccountHelper.getUserID());
                break;
            case 3://付款
                ToastUtils.showShort("暂不支持");
                break;
            case 4://提醒发货
                ToastUtils.showShort("已提醒卖家发货");
                orderAdapter.setRemindDelivery();
                break;
            case 5://查看配送

                ToastUtils.showShort("暂不支持");
                break;
            case 6://确认收货
                mViewModel.postBasicsConsignee(orderAdapter.getList().get(pos).getPayid(), UserAccountHelper.getUserID());
                break;
            //点击商品按钮
            case 7://晒图评论
                ToastUtils.showShort("暂不支持");
                break;
            case 8://前往核销
                Intent in = new Intent(this, VerificationActivity.class);
                startActivity(in);
                break;
            case 9://申请退货
                new ReturnGoodsDialog(this)
                        .builder()
                        .setOnSureClickListener((describe, imgList) -> {
                            mViewModel.postBasicsPayRefund(orderAdapter.getList().get(pos).getList().get(itemPos).getAdmin_id(),
                                    orderAdapter.getList().get(pos).getList().get(itemPos).getCommodity_id(), describe, imgList.get(0),
                                    orderAdapter.getList().get(pos).getOrder_id(), orderAdapter.getList().get(pos).getMy_out_trade_no(),
                                    orderAdapter.getList().get(pos).getList().get(itemPos).getPrice(), orderAdapter.getList().get(pos).getPrice(),
                                    orderAdapter.getList().get(pos).getList().get(itemPos).getType(), UserAccountHelper.getUserID());
                        })
                        .show();
                break;


        }
    }
}
